package com.gitee.jisong.superlogger.ctrl

import com.gitee.jisong.common.CtrlRes
import com.gitee.jisong.spring.annotation.GETPOST
import com.gitee.jisong.superlogger.dao.ApplicationsDAO
import com.gitee.jisong.superlogger.pojo.Applications
import org.apache.http.HttpEntity
import org.apache.http.client.methods.CloseableHttpResponse
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.BasicResponseHandler
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.impl.client.HttpClients
import org.apache.http.util.EntityUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController


@RestController
@RequestMapping("/super-logger/logger")
class loggersCtrl {

    @Autowired lateinit var applicationsDAO: ApplicationsDAO

    @GETPOST("/application/{id}")
    fun queryApplicationLoggers(@PathVariable("id") id : Long):CtrlRes {
        val app: Applications = applicationsDAO.selectById(id) ?:
        return CtrlRes(code = 400, msg = "应用不存在")
        val url: String = generateUrl(app)
        val client: CloseableHttpClient = HttpClients.createDefault()
        val httpGet = HttpGet(url)
        val execute: CloseableHttpResponse = client.execute(httpGet)
        execute.entity
        execute.statusLine.statusCode








        return CtrlRes(data = Applications())
    }


    private fun generateUrl(app: Applications):String{
        val prefix : String = if(app.isHttps==1){"https://"}else{"http://"}
        var url: StringBuilder = StringBuilder(prefix)
        url.append(app.appHost).append(":").append(app.appPort)
                .append("/xlogger/getLoggers")
        return url.toString()
    }
}